<?php
/**
 * +----------------------------------------------------------------------
 * | summer framework
 * +----------------------------------------------------------------------
 * | Copyright (c) 2020 https://github.com/suframe/think-admin All rights reserved.
 * +----------------------------------------------------------------------
 * | Author: summer <806115620@qq.com>  2020/5/23 10:40
 * +----------------------------------------------------------------------
 */
declare (strict_types=1);

namespace app\mall\logic;

use app\mall\enum\ClerkStatusEnum;
use app\mall\model\MallStore;
use app\mall\model\MallStoreClerk;
use app\mall\model\MallUserClerk;

/**
 * 店员service
 * Class StoreService
 * @package app\mall\logic
 */
class ClerkLogic extends Base
{

    public function getUserClerk(int $uid, int $storeId)
    {
        if (!$uid || !$storeId) {
            return [];
        }
        /** @var MallUserClerk $userClerk */
        $userClerk = MallUserClerk::where('store_id', $storeId)
            ->with(['clerk'])
            ->where('uid', $uid)
            ->find();
        $clerk = null;
        if ($userClerk) {
            $date = date('Y-m-d');
            if ($userClerk->last_date != $date) {
                $userClerk->last_date = $date;
                $userClerk->save();
            }
            $clerk = $userClerk->clerk;
            if ($clerk->status_enum->invalid()) {
                $clerk = null;
            }
        }
        if (!$clerk) {
            //如果无专属服务店员，分配

        }
    }

    protected function dispatchClerk($storeId)
    {
        $total = MallStoreClerk::where('store_id', $storeId)
            ->getTable()
            ->where('status', ClerkStatusEnum::VALID)
            ->or('rand()')
            ->limit(1)
            ->find();

    }

}